package cn.gdcp.Fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xinwenkehuduan.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.gdcp.Demo.Data;
import cn.gdcp.Demo.JsonRootBean;
import cn.gdcp.FragmentAdapter.NewsAdapter;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsFragment extends Fragment {
    private static final String TAG = "NewsFragment";
    private static final int MSG_GET_NEWS = 1001;
    private String baseUrl = "https://v.juhe.cn/toutiao/index";
    private final String key = "78971bf055a3d6770a3a5e8fe3acf9ab";
    private RecyclerView recyclerView;
    private NewsAdapter newsAdapter;
    private List<Data> newsList = new ArrayList<>();
    private Handler handler;
    private String type;
    private SwipeRefreshLayout swipeRefreshLayout;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news,container,false);

        recyclerView = view.findViewById(R.id.recyclerView);
        newsAdapter = new NewsAdapter(newsList);
        recyclerView.setAdapter(newsAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        swipeRefreshLayout = view.findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getNewsData();
            }
        });

        initHandler();
        getNewsData();
        return view;
    }

    private void getNewsData() {
        String url = baseUrl.concat("?type=").concat(type).concat("&key=").concat(key);
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonStr = response.body().string();
                Log.e(TAG,"onResponse: "+jsonStr);
                Gson gson = new Gson();
                JsonRootBean newsDataBean = gson.fromJson(jsonStr,JsonRootBean.class);
                newsList = newsDataBean.getResult().getData();
                handler.sendEmptyMessage(MSG_GET_NEWS);
            }
        });
    }

    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == MSG_GET_NEWS){
                    newsAdapter.changeDate(newsList);
                    swipeRefreshLayout.setRefreshing(false);
                    return true;
                }
                return false;
            }
        });

    }
}
